package part1.language_basis.book;

import java.util.ArrayList;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        ArrayList<Book> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        while (true){
            System.out.println("请选择你要进行的操作");
            System.out.println("1：添加图书");
            System.out.println("2：查看图书");
            System.out.println("3：查询图书");
            System.out.println("4：修改图书");
            System.out.println("5：删除图书");
            System.out.println("6：退出系统");
            String type = sc.next();
            switch (type){
                case "1":
                    addBook(list);
                    break;
                case "2":
                    findAllBook(list);
                    break;
                case "3":
                    findBookByName(list);
                    break;
                case "4":
                    updateBook(list);
                    break;
                case "5":
                    deleteBook(list);
                    break;
                case "6":
                    System.out.println("感谢使用，再见");
                    System.exit(0); //退出系统
                    break;
                default:
                    System.out.println("输入错误请重新输入");
                    break;
            }
        }
    }


    //删除图书
    private static void deleteBook(ArrayList<Book> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的图书编号");
        String bookId = scanner.next();
        for (int i = 0; i < list.size(); i++) {
            Book b = list.get(i);
            if (bookId.equals(b.getBookId())){
                list.remove(i);
                return;
            }
        }
        System.out.println("没有找到要删除的图书编号");
    }


    //修改图书
    private static void updateBook(ArrayList<Book> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改图书的编号");
        String bookId = scanner.next();
        for (int i = 0; i < list.size(); i++) {
            Book b = list.get(i);
            if (bookId.equals(b.getBookId())){
                System.out.println("请输入要修改的图书名");
                String name = scanner.next();
                System.out.println("请输入要修改的作者");
                String author = scanner.next();
                System.out.println("请输入要修改的价格");
                double price = scanner.nextDouble();
                Book newBook = new Book(bookId,name,author,price);
                list.set(i,newBook);
                return;
            }
        }
        System.out.println("没找到要修改的图书编号");
    }



    //根据图书名查询图书
    private static void findBookByName(ArrayList<Book> list){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入图书名");
        String name = scanner.next();
        for (int i = 0; i < list.size(); i++) {
            Book b = list.get(i);
            if (name.equals(b.getName())){
                System.out.println("编号\t"+"书名\t"+"作者\t"+"价格");
                System.out.println(b.getBookId()+"\t"+b.getName()+"\t"
                        +b.getAuthor()+"\t"+b.getPrice());
                return;
            }
        }
        System.out.println("没有次图书");

    }


    //查询所有图书
    private static void findAllBook(ArrayList<Book> list){
        if (list==null || list.size() ==0){
            System.out.println("图书为空");
        }
        System.out.println("编号\t"+"书名\t"+"作者\t"+"价格");
        for (int i = 0; i < list.size(); i++) {
            Book b = list.get(i);
            String bookId = b.getBookId();
            String name = b.getName();
            String author = b.getAuthor();
            double price = b.getPrice();
            System.out.println(b.getBookId()+"\t"+b.getName()+"\t"
            +b.getAuthor()+"\t"+b.getPrice());
        }
    }


    //新增图书
    private static void addBook(ArrayList<Book> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入图书信息");
        System.out.println("编号：");
        String bookId;
        while (true){
            bookId = sc.next();
            if (isExist(list,bookId)){
                System.out.println("图书编号已存在，请重新输入");
            }else {
                break;
            }
        }
        System.out.println("书名：");
        String name = sc.next();
        System.out.println("作者：");
        String author = sc.next();
        System.out.println("价格：");
        double price = sc.nextDouble();
        Book b = new Book(bookId,name,author,price);
        list.add(b);
    }

    private static boolean isExist(ArrayList<Book> list,String bookId){
        for (int i = 0; i < list.size(); i++) {
            Book b = list.get(i);
            if (bookId.equals(b.getBookId())){
                return true;
            }
        }
        return false;
    }
}
